package com.lcq.study.javabase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName StreamTest
 * @Description
 * @Author lichuanqi
 * @Date 2021/9/3 15:58
 * @Version 1.0
 **/
public class StreamTest {
    public static void main(String[] args) {
        //test1();
        //test2();
        ArrayList<A> as = new ArrayList<>();
        A a = new A();
        a.setAge(10);
        a.setName("a");
        a.setId("1");
        as.add(a);

        A a1 = new A();
        a1.setAge(10);
        a1.setName("b");
        a1.setId("2");
        as.add(a1);


        A a2 = new A();
        a2.setAge(10);
        a2.setName("c");
        a2.setId(null);
        as.add(a2);

        long count = as.stream().filter(item -> item.getAge() == 11)
                .map(A::getId).distinct().count();
        System.out.println(count);
    }

    private static void test2() {
        ArrayList<A> as = new ArrayList<>();
        A a = new A();
        a.setAge(1);
        a.setName("a");
        a.setId("1");
        as.add(a);

        A a1 = new A();
        a1.setAge(2);
        a1.setName("b");
        a1.setId("2");
        as.add(a1);

        A a2 = new A();
        a2.setAge(3);
        a2.setName("c");
        a2.setId("2");
        as.add(a2);
        List<A> collect = as.stream().distinct().collect(Collectors.toList());
        for (A a3 : collect) {
            System.out.println(a3.getName());
        }
    }

    private static void test1() {
        List<Integer> integers = Arrays.asList(3, 89, 17, 26, 78, 24, 1, 56);
        List<Integer> collect1 = integers.stream().sorted().collect(Collectors.toList());
        List<Integer> collect2 = integers.stream().sorted((i1, i2) -> i2.compareTo(i1)).collect(Collectors.toList());
        List<Integer> collect3 = integers.stream().sorted((i1, i2) -> i1.compareTo(i2)).collect(Collectors.toList());
        System.out.println(collect1);
        System.out.println(collect2);
        System.out.println(collect3);
    }

    static class A {
        private String name;
        private String id;
        private Integer age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            A a = (A) o;
            return Objects.equals(id, a.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }


}
